package de.farbstreich.tozen2.client;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import de.farbstreich.tozen2.client.GameData.Player;

public class ClientGUIChat extends JPanel {

	private static final long serialVersionUID = 1L;
	private JTextArea messageTextArea = null;
	private JButton sendMessageButton = null;
	private JComboBox playersComboBox = null;
	private JTextField messageTextField = null;
	private ClientGUIInput guiInput;

	public ClientGUIChat() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		GridBagConstraints gridMessageTextField = new GridBagConstraints();
		gridMessageTextField.fill = GridBagConstraints.BOTH;
		gridMessageTextField.gridy = 1;
		gridMessageTextField.weightx = 1.0;
		gridMessageTextField.insets = new Insets(0, 10, 10, 10);
		gridMessageTextField.gridwidth = 2;
		gridMessageTextField.anchor = GridBagConstraints.CENTER;
		gridMessageTextField.gridx = 1;
		
		GridBagConstraints gridPlayersComboBox = new GridBagConstraints();
		gridPlayersComboBox.fill = GridBagConstraints.BOTH;
		gridPlayersComboBox.gridy = 2;
		gridPlayersComboBox.weightx = 0.25;
		gridPlayersComboBox.gridwidth = 1;
		gridPlayersComboBox.insets = new Insets(0, 10, 10, 10);
		gridPlayersComboBox.anchor = GridBagConstraints.NORTHWEST;
		gridPlayersComboBox.gridx = 1;
		
		GridBagConstraints gridSendMessageButton = new GridBagConstraints();
		gridSendMessageButton.gridx = 2;
		gridSendMessageButton.insets = new Insets(0, 10, 10, 10);
		gridSendMessageButton.anchor = GridBagConstraints.NORTHEAST;
		gridSendMessageButton.gridy = 2;
		
		GridBagConstraints gridMessageTextArea = new GridBagConstraints();
		gridMessageTextArea.fill = GridBagConstraints.BOTH;
		gridMessageTextArea.gridy = 0;
		gridMessageTextArea.weightx = 1.0;
		gridMessageTextArea.weighty = 1.0;
		gridMessageTextArea.gridwidth = 3;
		gridMessageTextArea.insets = new Insets(10, 10, 10, 10);
		gridMessageTextArea.gridx = 0;
		
		
		this.setSize(300, 600);
		this.setLayout(new GridBagLayout());
		this.add(new JScrollPane(getMessageTextArea()), gridMessageTextArea);
		this.add(getSendMessageButton(), gridSendMessageButton);
		this.add(getPlayersComboBox(), gridPlayersComboBox);
		this.add(getMessageTextField(), gridMessageTextField);
	}

	/**
	 * This method initializes jTextArea	
	 * 	
	 * @return javax.swing.JTextArea	
	 */
	JTextArea getMessageTextArea() {
		if (messageTextArea == null) {
			messageTextArea = new JTextArea();
			messageTextArea.setEditable(false);
			messageTextArea.setLineWrap(true);
			Formator.formateComponent(messageTextArea);
			
		}
		return messageTextArea;
	}

	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getSendMessageButton() {
		if (sendMessageButton == null) {
			sendMessageButton = new JButton();
			sendMessageButton.setText("senden");
			Formator.formateButton(sendMessageButton, 100);
			sendMessageButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					assert guiInput == null : "ClientGUIChat Error: no GUI input";
					if(messageTextField.getText().isEmpty()) {return;}
					guiInput.sendMessage(messageTextField.getText(), (Player) playersComboBox.getSelectedItem());
					messageTextField.setText("");
				}
			});
			
		}
		return sendMessageButton;
	}

	/**
	 * This method initializes jComboBox	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	public  JComboBox getPlayersComboBox() {
		if (playersComboBox == null) {
			playersComboBox = new JComboBox();
			playersComboBox.setEditable(false);
		}
		Formator.formateComponent(playersComboBox);
		return playersComboBox;
	}

	/**
	 * This method initializes jTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	public  JTextField getMessageTextField() {
		if (messageTextField == null) {
			messageTextField = new JTextField();
		}
		Formator.formateComponent(messageTextField);
		return messageTextField;
	}

	public void setInput(ClientGUIInput guiInput) {
		this.guiInput = guiInput;	
	}

}
